package com.smartao.base.employee;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.FetchMode;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.smartao.core.dao.BaseEntityDao;

@Repository
public class EmployeeDaoImpl extends BaseEntityDao<Employee, Long> implements EmployeeDao {

	@Override
	public Employee findByAccount(String account) {
		Criteria criteria = getSession().createCriteria(Employee.class);
		criteria.add(Restrictions.eq("account", account));
		criteria.setFetchMode("roles", FetchMode.JOIN);
		criteria.setFetchMode("positions", FetchMode.JOIN);
        return (Employee) criteria.uniqueResult();
	}

	@Override
	public List<Employee> findByDepartment(Long id) {
		return findBy("department", id);
	}
	
	@Override
	public Long countByDepartment(Long departmentId){
		String hql = "SELECT COUNT(*) FROM Employee WHERE department.id=?";
		return findUnique(hql, departmentId);
	}

	@Override
	public void updateAccount(Long employeeId, String account, String password) {
		String hql = "UPDATE Employee SET account =:account, password=:password where id=:id";
		createQuery(hql).setString("account", account)
				.setString("password", password)
				.setLong("id", employeeId)
				.executeUpdate();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Employee> findByIds(Long[] ids) {
		Criteria criteria = getSession().createCriteria(Employee.class);
		criteria.add(Restrictions.in("id", ids));
		return criteria.list();
	}
	
}
